package nbp.currency.gui;

import java.awt.GridLayout;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import nbp.currency.logic.TypeOfExchange;

import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.JXTextField;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

public class NBPCurrencyPanel extends JPanel {

	private JComboBox tableTypeComboBox;
	private JXDatePicker dateFrom;
	private JXDatePicker dateTo;
	private JXTable exchangeTable;
	private JButton submitButton;
	private JXTextField filterTextBox;

	private static final long serialVersionUID = 6358781602956534118L;

	public NBPCurrencyPanel() {
		super(new GridLayout());

		JPanel panel = new JPanel();
		FormLayout formLayout = new FormLayout(
				"2dlu,p,2dlu,p, 2dlu,p,2dlu, p, 2dlu, p, 2dlu,p,2dlu,p,2dlu",
				"2dlu,fill:15dlu, 2dlu,fill:15dlu, 2dlu,p:g,2dlu");
		panel.setLayout(formLayout);
		CellConstraints c = new CellConstraints();
		panel.add(new JLabel("Data od:"), c.xy(2, 2));
		panel.add(getDateTo(), c.xy(4, 2));
		panel.add(new JLabel("Data do:"), c.xy(6, 2));
		panel.add(getDateFrom(), c.xy(8, 2));
		panel.add(new JLabel("Typ tabeli:"), c.xy(10, 2));
		panel.add(getTableTypeComboBox(), c.xy(12, 2));
		panel.add(getSubmitButton(), c.xy(14, 2));
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(getTable());
		panel.add(new JLabel("Filtruj:"), c.xy(2, 4));
		panel.add(getFilterTextBox(), c.xyw(4, 4,10));
		panel.add(scrollPane, c.xyw(2,6, 13));
		add(panel);
	}

	public JComboBox getTableTypeComboBox() {
		if (tableTypeComboBox == null) {
			tableTypeComboBox = new JComboBox(TypeOfExchange.values()) {
			};
		}

		return tableTypeComboBox;
	}

	public JXDatePicker getDateFrom() {
		if (dateFrom == null) {
			dateFrom = new JXDatePicker();
			dateFrom.setFormats(SimpleDateFormat.getDateInstance());
		}
		return dateFrom;
	}

	public JXDatePicker getDateTo() {
		if (dateTo == null) {
			dateTo = new JXDatePicker();
			dateTo.setFormats(SimpleDateFormat.getDateInstance());
		}
		return dateTo;
	}

	public JXTable getTable(){
		if(exchangeTable==null){
			exchangeTable = new JXTable();
			exchangeTable.setEditable(true);
		}
		return exchangeTable;
	}
	
	public JButton getSubmitButton(){
		if(submitButton==null){
			submitButton = new JButton("Pobierz");
		}
		return submitButton;
	}
	
	public JXTextField getFilterTextBox(){
		if(filterTextBox == null){
			filterTextBox = new JXTextField();
		}
		return filterTextBox;
	}
}
